{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "468553c2-cfeb-4aad-abb8-801614f9e070",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import datetime as dt\n",
    "from jq_utils import sdk_auth\n",
    "import numpy as np\n",
    "from tqdm.auto import tqdm\n",
    "sdk_auth()\n",
    "\n",
    "times = [935, 940, 945, 950, 955, 1000, 1005, 1010, 1015,\n",
    "         1020, 1025, 1030, 1035, 1040, 1045, 1050, 1055,\n",
    "         1100, 1105, 1110, 1115, 1120, 1125, 1130, 1305,\n",
    "         1310, 1315, 1320, 1325, 1330, 1335, 1340, 1345,\n",
    "         1350, 1355, 1400, 1405, 1410, 1415, 1420, 1425,\n",
    "         1430, 1435, 1440, 1445, 1450, 1455, 1500]\n",
    "\n",
    "\n",
    "class Market:\n",
    "    def __init__(self, data):\n",
    "        self.action_space = ['B', 'S', 'W']  # 买进、卖出、观望\n",
    "        self.n_actions = len(self.action_space)\n",
    "        self.data = data  # 935 940 ... 1500 48根K线的数据\n",
    "        self.time = 935\n",
    "        pass\n",
    "\n",
    "    def step(self, action):\n",
    "        # 要知道当前在那个状态即时间点,用下一时间点的R（收益）作为\n",
    "        # 当前采取action的reward\n",
    "        tix = times.index(self.time)\n",
    "        nix = tix + 1\n",
    "        if self.time == 1500:\n",
    "            reward = 0\n",
    "            done = True\n",
    "            s_ = 'terminal'\n",
    "            # print('time is over.')\n",
    "        else:\n",
    "            reward = self.data.R.iloc[nix]\n",
    "            done = False\n",
    "            s_ = times[nix]\n",
    "        if action == 'B':\n",
    "            pass\n",
    "        elif action == 'S':\n",
    "            # 当R为-的时候，选择S，应该是正奖励\n",
    "            reward = reward * -1\n",
    "        else:\n",
    "            # 选择观望，既不亏损也不会盈利，但会损失机会成本\n",
    "            # 我们当前对观望的决策持客观态度，reward=0，这\n",
    "            # 可能需要在不同的大盘行情下适时调整\n",
    "            reward = 0\n",
    "            pass\n",
    "        self.time = s_\n",
    "        return s_, reward, done\n",
    "        pass\n",
    "\n",
    "    def reset(self):\n",
    "        self.time = 935\n",
    "        return self.time\n",
    "        pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "e2fc2046-5b59-40cf-bdcf-f0df6e388d93",
   "metadata": {},
   "outputs": [],
   "source": [
    "class QLearning:\n",
    "\n",
    "    #Agent\n",
    "\n",
    "\n",
    "    def __init__(self, actions, q_table=None, learning_rate=0.01,\n",
    "                 discount_factor=0.9, e_greedy=0.1):\n",
    "        self.actions = actions  # action 列表\n",
    "        self.lr = learning_rate  # 学习速率\n",
    "        self.gamma = discount_factor  # 折扣因子\n",
    "        self.epsilon = e_greedy  # 贪婪度\n",
    "        # 列是action。\n",
    "        if q_table is None:\n",
    "            self.q_table = pd.DataFrame(columns=self.actions, dtype=np.float32)  # Q 表\n",
    "        else:\n",
    "            self.q_table = q_table\n",
    "\n",
    "    # 检测 q_table 中有没有这个 state\n",
    "    # 如果还没有当前 state, 那我们就插入一组全 0 数据, 作为这个 state 的所有 action 的初始值\n",
    "    def check_state_exist(self, state):\n",
    "        # state对应每一行，如果不在Q表中。\n",
    "        if state not in self.q_table.index:\n",
    "            # 插入一组全 0 数据，给每个action赋值为0\n",
    "            self.q_table = self.q_table.append(\n",
    "                pd.Series(\n",
    "                    [0] * len(self.actions),\n",
    "                    index=self.q_table.columns,\n",
    "                    name=state,\n",
    "                )\n",
    "            )\n",
    "\n",
    "    # 根据 state 来选择 action\n",
    "    def choose_action(self, state):\n",
    "        self.check_state_exist(state)  # 检测此 state 是否在 q_table 中存在\n",
    "        # 选行为，用 Epsilon Greedy 贪婪方法\n",
    "        if np.random.uniform() < self.epsilon:\n",
    "            # 随机选择 action\n",
    "            action = np.random.choice(self.actions)\n",
    "        else:  # 选择 Q 值最高的 action\n",
    "            state_action = self.q_table.loc[state, :]\n",
    "            # 同一个 state, 可能会有多个相同的 Q action 值, 所以我们乱序一下\n",
    "            state_action = state_action.reindex(np.random.permutation(state_action.index))\n",
    "            # 每一行中取到Q值最大的那个\n",
    "            action = state_action.idxmax()\n",
    "        return action\n",
    "\n",
    "    # 学习。更新 Q 表中的值\n",
    "    def learn(self, s, a, r, s_):\n",
    "        # s_是下一个状态\n",
    "        self.check_state_exist(s_)  # 检测 q_table 中是否存在 s_\n",
    "\n",
    "        # Q(S,A) <- Q(S,A)+a*[R+v*max(Q(S',a))-Q(S,A)]\n",
    "\n",
    "        q_predict = self.q_table.loc[s, a]  # 根据 Q 表得到的 估计（predict）值\n",
    "\n",
    "        # q_target 是现实值\n",
    "        if s_ != 'terminal':  # 下个 state 不是 终止符\n",
    "            q_target = r + self.gamma * self.q_table.loc[s_, :].max()\n",
    "        else:\n",
    "            q_target = r  # 下个 state 是 终止符\n",
    "\n",
    "        # 更新 Q 表中 state-action 的值\n",
    "        self.q_table.loc[s, a] += self.lr * (q_target - q_predict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0efb7dca-b47b-4f94-85bc-8e4f46725c8e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "774b77334e6345b2b73dbfb68305d1e9",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/200 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "def update(data, q_table=None):\n",
    "    env = Market(data)\n",
    "    RL = QLearning(actions=env.action_space, q_table=q_table)\n",
    "\n",
    "    for episode in range(100):\n",
    "        # 初始化 state（状态）\n",
    "        state = env.reset()\n",
    "\n",
    "        step_count = 0  # 记录走过的步数\n",
    "\n",
    "        while True:\n",
    "            # 更新可视化环境\n",
    "            # env.render()\n",
    "            # RL 大脑根据 state 挑选 action\n",
    "            action = RL.choose_action(str(state))\n",
    "            # 探索者在环境中实施这个 action, 并得到环境返回的下一个 state, reward 和 done (是否到了1500)\n",
    "            state_, reward, done = env.step(action)\n",
    "            step_count += 1  # 增加步数\n",
    "            # 机器人大脑从这个过渡（transition） (state, action, reward, state_) 中学习\n",
    "            RL.learn(str(state), action, reward, str(state_))\n",
    "            # 机器人移动到下一个 state\n",
    "            state = state_\n",
    "            # 如果时间到了1500, 这回合就结束了，或者是某个止损条件达到了\n",
    "            if done:\n",
    "                # print(\"回合 {} 结束. 总步数 : {}\\n\".format(episode + 1, step_count))\n",
    "                break\n",
    "\n",
    "    # print('模拟交易结束了。')\n",
    "    # print('\\nQ 表:')\n",
    "    # print(RL.q_table)\n",
    "    return RL.q_table\n",
    "\n",
    "\n",
    "def train():\n",
    "    code = '000001'  # 上证指数\n",
    "    sd = dt.datetime(2018, 10, 1)\n",
    "    ed = dt.datetime(2018, 11, 1)\n",
    "    # 我已经把从jqdata读取到了数据存在了本地，这里只是读取出来\n",
    "#     data = md().read_data('index_min5', stock_code=code,\n",
    "#                           date={'gte': sd, 'lt': ed},\n",
    "#                           field={'_id': 0, 'time': 1, 'close': 1, 'date': 1})\n",
    "    data = df\n",
    "    data = data.sort_values(['date', 'time'], ascending=False)\n",
    "    # 计算每根K线收盘时未来三根K线的涨跌幅\n",
    "    data['R'] = (data.close.shift(3) / data.close - 1) * 100\n",
    "    data.fillna(0, inplace=True)\n",
    "    data = data.round({'R': 3})\n",
    "    data = data.sort_values(['date', 'time'], ascending=True)\n",
    "    qtb = None\n",
    "    for k, g in tqdm(data.groupby(['date'])):\n",
    "#         print('train to:', k)\n",
    "        try:\n",
    "            # 开始一天一天的训练\n",
    "            qtb = update(g, qtb)\n",
    "        except Exception as e:\n",
    "            print(e)\n",
    "#         print('\\nQ 表:')\n",
    "#         print(qtb)\n",
    "    qtb['time'] = qtb.index\n",
    "    qtb.to_csv(path_or_buf='model_param/qtb({})_{}.csv'.\n",
    "               format(code, sd.strftime('%Y_%m_%d')), index=False)\n",
    "    pass\n",
    "\n",
    "\n",
    "train()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d44b2aad-9b58-4090-93b1-0600d4509476",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "auth success \n"
     ]
    }
   ],
   "source": [
    "from jqdatasdk import *\n",
    "from jq_utils import sdk_auth\n",
    "import pandas as pd\n",
    "import datetime as dt\n",
    "sdk_auth()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "5a0ee799-371a-4825-a655-6c05b4a371c2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>dt</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>volume</th>\n",
       "      <th>money</th>\n",
       "      <th>date</th>\n",
       "      <th>time</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2020-10-27 09:35:00</td>\n",
       "      <td>3240.74</td>\n",
       "      <td>3245.74</td>\n",
       "      <td>3238.45</td>\n",
       "      <td>3239.59</td>\n",
       "      <td>1.043432e+09</td>\n",
       "      <td>1.489245e+10</td>\n",
       "      <td>2020-10-27</td>\n",
       "      <td>09:35</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2020-10-27 09:40:00</td>\n",
       "      <td>3239.52</td>\n",
       "      <td>3246.45</td>\n",
       "      <td>3239.38</td>\n",
       "      <td>3246.40</td>\n",
       "      <td>6.490323e+08</td>\n",
       "      <td>9.377573e+09</td>\n",
       "      <td>2020-10-27</td>\n",
       "      <td>09:40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2020-10-27 09:45:00</td>\n",
       "      <td>3247.20</td>\n",
       "      <td>3247.80</td>\n",
       "      <td>3243.92</td>\n",
       "      <td>3247.03</td>\n",
       "      <td>5.676211e+08</td>\n",
       "      <td>8.150270e+09</td>\n",
       "      <td>2020-10-27</td>\n",
       "      <td>09:45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2020-10-27 09:50:00</td>\n",
       "      <td>3246.14</td>\n",
       "      <td>3250.70</td>\n",
       "      <td>3243.78</td>\n",
       "      <td>3249.89</td>\n",
       "      <td>5.536402e+08</td>\n",
       "      <td>7.783643e+09</td>\n",
       "      <td>2020-10-27</td>\n",
       "      <td>09:50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2020-10-27 09:55:00</td>\n",
       "      <td>3250.08</td>\n",
       "      <td>3255.57</td>\n",
       "      <td>3250.08</td>\n",
       "      <td>3255.15</td>\n",
       "      <td>5.173200e+08</td>\n",
       "      <td>7.514062e+09</td>\n",
       "      <td>2020-10-27</td>\n",
       "      <td>09:55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9595</th>\n",
       "      <td>2021-08-17 14:40:00</td>\n",
       "      <td>3451.24</td>\n",
       "      <td>3452.63</td>\n",
       "      <td>3445.19</td>\n",
       "      <td>3445.29</td>\n",
       "      <td>7.347060e+08</td>\n",
       "      <td>1.022136e+10</td>\n",
       "      <td>2021-08-17</td>\n",
       "      <td>14:40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9596</th>\n",
       "      <td>2021-08-17 14:45:00</td>\n",
       "      <td>3445.19</td>\n",
       "      <td>3445.19</td>\n",
       "      <td>3438.66</td>\n",
       "      <td>3438.70</td>\n",
       "      <td>1.077693e+09</td>\n",
       "      <td>1.431364e+10</td>\n",
       "      <td>2021-08-17</td>\n",
       "      <td>14:45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9597</th>\n",
       "      <td>2021-08-17 14:50:00</td>\n",
       "      <td>3438.36</td>\n",
       "      <td>3440.48</td>\n",
       "      <td>3438.17</td>\n",
       "      <td>3440.48</td>\n",
       "      <td>9.899081e+08</td>\n",
       "      <td>1.332597e+10</td>\n",
       "      <td>2021-08-17</td>\n",
       "      <td>14:50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9598</th>\n",
       "      <td>2021-08-17 14:55:00</td>\n",
       "      <td>3440.39</td>\n",
       "      <td>3445.52</td>\n",
       "      <td>3440.35</td>\n",
       "      <td>3445.13</td>\n",
       "      <td>1.057331e+09</td>\n",
       "      <td>1.418659e+10</td>\n",
       "      <td>2021-08-17</td>\n",
       "      <td>14:55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9599</th>\n",
       "      <td>2021-08-17 15:00:00</td>\n",
       "      <td>3445.21</td>\n",
       "      <td>3446.98</td>\n",
       "      <td>3444.95</td>\n",
       "      <td>3446.98</td>\n",
       "      <td>9.534231e+08</td>\n",
       "      <td>1.265154e+10</td>\n",
       "      <td>2021-08-17</td>\n",
       "      <td>15:00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>9600 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                       dt     open     high      low    close        volume  \\\n",
       "0     2020-10-27 09:35:00  3240.74  3245.74  3238.45  3239.59  1.043432e+09   \n",
       "1     2020-10-27 09:40:00  3239.52  3246.45  3239.38  3246.40  6.490323e+08   \n",
       "2     2020-10-27 09:45:00  3247.20  3247.80  3243.92  3247.03  5.676211e+08   \n",
       "3     2020-10-27 09:50:00  3246.14  3250.70  3243.78  3249.89  5.536402e+08   \n",
       "4     2020-10-27 09:55:00  3250.08  3255.57  3250.08  3255.15  5.173200e+08   \n",
       "...                   ...      ...      ...      ...      ...           ...   \n",
       "9595  2021-08-17 14:40:00  3451.24  3452.63  3445.19  3445.29  7.347060e+08   \n",
       "9596  2021-08-17 14:45:00  3445.19  3445.19  3438.66  3438.70  1.077693e+09   \n",
       "9597  2021-08-17 14:50:00  3438.36  3440.48  3438.17  3440.48  9.899081e+08   \n",
       "9598  2021-08-17 14:55:00  3440.39  3445.52  3440.35  3445.13  1.057331e+09   \n",
       "9599  2021-08-17 15:00:00  3445.21  3446.98  3444.95  3446.98  9.534231e+08   \n",
       "\n",
       "             money        date   time  \n",
       "0     1.489245e+10  2020-10-27  09:35  \n",
       "1     9.377573e+09  2020-10-27  09:40  \n",
       "2     8.150270e+09  2020-10-27  09:45  \n",
       "3     7.783643e+09  2020-10-27  09:50  \n",
       "4     7.514062e+09  2020-10-27  09:55  \n",
       "...            ...         ...    ...  \n",
       "9595  1.022136e+10  2021-08-17  14:40  \n",
       "9596  1.431364e+10  2021-08-17  14:45  \n",
       "9597  1.332597e+10  2021-08-17  14:50  \n",
       "9598  1.418659e+10  2021-08-17  14:55  \n",
       "9599  1.265154e+10  2021-08-17  15:00  \n",
       "\n",
       "[9600 rows x 9 columns]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "code = '000001.XSHG'\n",
    "count = 9600\n",
    "freq = '5m'\n",
    "# end_dt = now\n",
    "end_dt = dt.datetime.strptime('2021-08-17 15:01:00', '%Y-%m-%d %H:%M:%S')\n",
    "# df = get_bars(code,count = count, unit=freq,\n",
    "# fields=['date','open','high','low','close', 'volume', 'money'],\n",
    "#               end_dt=end_dt, fq_ref_date=None,df=True)\n",
    "\n",
    "# df.rename(columns={'date': 'dt'}, inplace=True)\n",
    "# df['date'] = df['dt'].apply(lambda x: x.strftime('%Y-%m-%d'))\n",
    "# df['time'] = df['dt'].apply(lambda x: x.strftime('%H:%M'))\n",
    "# df.to_csv('hour.csv', index = False)\n",
    "\n",
    "df = pd.read_csv('hour.csv')\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "430e6864-1d27-4196-a587-530644b934a0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "0b6406f6-c80f-41bf-a761-cb17fd224584",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9600"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "480 * 20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "3840858f-b4b5-4102-b441-aaf4ddd1d673",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>volume</th>\n",
       "      <th>money</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2020-10-27 09:35:00</td>\n",
       "      <td>3240.74</td>\n",
       "      <td>3245.74</td>\n",
       "      <td>3238.45</td>\n",
       "      <td>3239.59</td>\n",
       "      <td>1.043432e+09</td>\n",
       "      <td>1.489245e+10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2020-10-27 09:40:00</td>\n",
       "      <td>3239.52</td>\n",
       "      <td>3246.45</td>\n",
       "      <td>3239.38</td>\n",
       "      <td>3246.40</td>\n",
       "      <td>6.490323e+08</td>\n",
       "      <td>9.377573e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2020-10-27 09:45:00</td>\n",
       "      <td>3247.20</td>\n",
       "      <td>3247.80</td>\n",
       "      <td>3243.92</td>\n",
       "      <td>3247.03</td>\n",
       "      <td>5.676211e+08</td>\n",
       "      <td>8.150270e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2020-10-27 09:50:00</td>\n",
       "      <td>3246.14</td>\n",
       "      <td>3250.70</td>\n",
       "      <td>3243.78</td>\n",
       "      <td>3249.89</td>\n",
       "      <td>5.536402e+08</td>\n",
       "      <td>7.783643e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2020-10-27 09:55:00</td>\n",
       "      <td>3250.08</td>\n",
       "      <td>3255.57</td>\n",
       "      <td>3250.08</td>\n",
       "      <td>3255.15</td>\n",
       "      <td>5.173200e+08</td>\n",
       "      <td>7.514062e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9595</th>\n",
       "      <td>2021-08-17 14:40:00</td>\n",
       "      <td>3451.24</td>\n",
       "      <td>3452.63</td>\n",
       "      <td>3445.19</td>\n",
       "      <td>3445.29</td>\n",
       "      <td>7.347060e+08</td>\n",
       "      <td>1.022136e+10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9596</th>\n",
       "      <td>2021-08-17 14:45:00</td>\n",
       "      <td>3445.19</td>\n",
       "      <td>3445.19</td>\n",
       "      <td>3438.66</td>\n",
       "      <td>3438.70</td>\n",
       "      <td>1.077693e+09</td>\n",
       "      <td>1.431364e+10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9597</th>\n",
       "      <td>2021-08-17 14:50:00</td>\n",
       "      <td>3438.36</td>\n",
       "      <td>3440.48</td>\n",
       "      <td>3438.17</td>\n",
       "      <td>3440.48</td>\n",
       "      <td>9.899081e+08</td>\n",
       "      <td>1.332597e+10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9598</th>\n",
       "      <td>2021-08-17 14:55:00</td>\n",
       "      <td>3440.39</td>\n",
       "      <td>3445.52</td>\n",
       "      <td>3440.35</td>\n",
       "      <td>3445.13</td>\n",
       "      <td>1.057331e+09</td>\n",
       "      <td>1.418659e+10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9599</th>\n",
       "      <td>2021-08-17 15:00:00</td>\n",
       "      <td>3445.21</td>\n",
       "      <td>3446.98</td>\n",
       "      <td>3444.95</td>\n",
       "      <td>3446.98</td>\n",
       "      <td>9.534231e+08</td>\n",
       "      <td>1.265154e+10</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>9600 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                    date     open     high      low    close        volume  \\\n",
       "0    2020-10-27 09:35:00  3240.74  3245.74  3238.45  3239.59  1.043432e+09   \n",
       "1    2020-10-27 09:40:00  3239.52  3246.45  3239.38  3246.40  6.490323e+08   \n",
       "2    2020-10-27 09:45:00  3247.20  3247.80  3243.92  3247.03  5.676211e+08   \n",
       "3    2020-10-27 09:50:00  3246.14  3250.70  3243.78  3249.89  5.536402e+08   \n",
       "4    2020-10-27 09:55:00  3250.08  3255.57  3250.08  3255.15  5.173200e+08   \n",
       "...                  ...      ...      ...      ...      ...           ...   \n",
       "9595 2021-08-17 14:40:00  3451.24  3452.63  3445.19  3445.29  7.347060e+08   \n",
       "9596 2021-08-17 14:45:00  3445.19  3445.19  3438.66  3438.70  1.077693e+09   \n",
       "9597 2021-08-17 14:50:00  3438.36  3440.48  3438.17  3440.48  9.899081e+08   \n",
       "9598 2021-08-17 14:55:00  3440.39  3445.52  3440.35  3445.13  1.057331e+09   \n",
       "9599 2021-08-17 15:00:00  3445.21  3446.98  3444.95  3446.98  9.534231e+08   \n",
       "\n",
       "             money  \n",
       "0     1.489245e+10  \n",
       "1     9.377573e+09  \n",
       "2     8.150270e+09  \n",
       "3     7.783643e+09  \n",
       "4     7.514062e+09  \n",
       "...            ...  \n",
       "9595  1.022136e+10  \n",
       "9596  1.431364e+10  \n",
       "9597  1.332597e+10  \n",
       "9598  1.418659e+10  \n",
       "9599  1.265154e+10  \n",
       "\n",
       "[9600 rows x 7 columns]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "1429134e-4dad-4784-9a47-47445940dbd3",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "bdd8a345-13f4-45f1-b941-adda61ed82ea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>dt</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>volume</th>\n",
       "      <th>money</th>\n",
       "      <th>date</th>\n",
       "      <th>time</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2020-10-27 09:35:00</td>\n",
       "      <td>3240.74</td>\n",
       "      <td>3245.74</td>\n",
       "      <td>3238.45</td>\n",
       "      <td>3239.59</td>\n",
       "      <td>1.043432e+09</td>\n",
       "      <td>1.489245e+10</td>\n",
       "      <td>2020-10-27</td>\n",
       "      <td>09:35</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2020-10-27 09:40:00</td>\n",
       "      <td>3239.52</td>\n",
       "      <td>3246.45</td>\n",
       "      <td>3239.38</td>\n",
       "      <td>3246.40</td>\n",
       "      <td>6.490323e+08</td>\n",
       "      <td>9.377573e+09</td>\n",
       "      <td>2020-10-27</td>\n",
       "      <td>09:40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2020-10-27 09:45:00</td>\n",
       "      <td>3247.20</td>\n",
       "      <td>3247.80</td>\n",
       "      <td>3243.92</td>\n",
       "      <td>3247.03</td>\n",
       "      <td>5.676211e+08</td>\n",
       "      <td>8.150270e+09</td>\n",
       "      <td>2020-10-27</td>\n",
       "      <td>09:45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2020-10-27 09:50:00</td>\n",
       "      <td>3246.14</td>\n",
       "      <td>3250.70</td>\n",
       "      <td>3243.78</td>\n",
       "      <td>3249.89</td>\n",
       "      <td>5.536402e+08</td>\n",
       "      <td>7.783643e+09</td>\n",
       "      <td>2020-10-27</td>\n",
       "      <td>09:50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2020-10-27 09:55:00</td>\n",
       "      <td>3250.08</td>\n",
       "      <td>3255.57</td>\n",
       "      <td>3250.08</td>\n",
       "      <td>3255.15</td>\n",
       "      <td>5.173200e+08</td>\n",
       "      <td>7.514062e+09</td>\n",
       "      <td>2020-10-27</td>\n",
       "      <td>09:55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9595</th>\n",
       "      <td>2021-08-17 14:40:00</td>\n",
       "      <td>3451.24</td>\n",
       "      <td>3452.63</td>\n",
       "      <td>3445.19</td>\n",
       "      <td>3445.29</td>\n",
       "      <td>7.347060e+08</td>\n",
       "      <td>1.022136e+10</td>\n",
       "      <td>2021-08-17</td>\n",
       "      <td>14:40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9596</th>\n",
       "      <td>2021-08-17 14:45:00</td>\n",
       "      <td>3445.19</td>\n",
       "      <td>3445.19</td>\n",
       "      <td>3438.66</td>\n",
       "      <td>3438.70</td>\n",
       "      <td>1.077693e+09</td>\n",
       "      <td>1.431364e+10</td>\n",
       "      <td>2021-08-17</td>\n",
       "      <td>14:45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9597</th>\n",
       "      <td>2021-08-17 14:50:00</td>\n",
       "      <td>3438.36</td>\n",
       "      <td>3440.48</td>\n",
       "      <td>3438.17</td>\n",
       "      <td>3440.48</td>\n",
       "      <td>9.899081e+08</td>\n",
       "      <td>1.332597e+10</td>\n",
       "      <td>2021-08-17</td>\n",
       "      <td>14:50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9598</th>\n",
       "      <td>2021-08-17 14:55:00</td>\n",
       "      <td>3440.39</td>\n",
       "      <td>3445.52</td>\n",
       "      <td>3440.35</td>\n",
       "      <td>3445.13</td>\n",
       "      <td>1.057331e+09</td>\n",
       "      <td>1.418659e+10</td>\n",
       "      <td>2021-08-17</td>\n",
       "      <td>14:55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9599</th>\n",
       "      <td>2021-08-17 15:00:00</td>\n",
       "      <td>3445.21</td>\n",
       "      <td>3446.98</td>\n",
       "      <td>3444.95</td>\n",
       "      <td>3446.98</td>\n",
       "      <td>9.534231e+08</td>\n",
       "      <td>1.265154e+10</td>\n",
       "      <td>2021-08-17</td>\n",
       "      <td>15:00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>9600 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                      dt     open     high      low    close        volume  \\\n",
       "0    2020-10-27 09:35:00  3240.74  3245.74  3238.45  3239.59  1.043432e+09   \n",
       "1    2020-10-27 09:40:00  3239.52  3246.45  3239.38  3246.40  6.490323e+08   \n",
       "2    2020-10-27 09:45:00  3247.20  3247.80  3243.92  3247.03  5.676211e+08   \n",
       "3    2020-10-27 09:50:00  3246.14  3250.70  3243.78  3249.89  5.536402e+08   \n",
       "4    2020-10-27 09:55:00  3250.08  3255.57  3250.08  3255.15  5.173200e+08   \n",
       "...                  ...      ...      ...      ...      ...           ...   \n",
       "9595 2021-08-17 14:40:00  3451.24  3452.63  3445.19  3445.29  7.347060e+08   \n",
       "9596 2021-08-17 14:45:00  3445.19  3445.19  3438.66  3438.70  1.077693e+09   \n",
       "9597 2021-08-17 14:50:00  3438.36  3440.48  3438.17  3440.48  9.899081e+08   \n",
       "9598 2021-08-17 14:55:00  3440.39  3445.52  3440.35  3445.13  1.057331e+09   \n",
       "9599 2021-08-17 15:00:00  3445.21  3446.98  3444.95  3446.98  9.534231e+08   \n",
       "\n",
       "             money        date   time  \n",
       "0     1.489245e+10  2020-10-27  09:35  \n",
       "1     9.377573e+09  2020-10-27  09:40  \n",
       "2     8.150270e+09  2020-10-27  09:45  \n",
       "3     7.783643e+09  2020-10-27  09:50  \n",
       "4     7.514062e+09  2020-10-27  09:55  \n",
       "...            ...         ...    ...  \n",
       "9595  1.022136e+10  2021-08-17  14:40  \n",
       "9596  1.431364e+10  2021-08-17  14:45  \n",
       "9597  1.332597e+10  2021-08-17  14:50  \n",
       "9598  1.418659e+10  2021-08-17  14:55  \n",
       "9599  1.265154e+10  2021-08-17  15:00  \n",
       "\n",
       "[9600 rows x 9 columns]"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "ad3ae658-49f5-47d1-9558-8ded326ae633",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2021-08-17'"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "end_dt.strftime('%Y-%m-%d')\n",
    "# '%Y-%m-%d %H:%M:%S'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "d2408a3e-dbe0-415d-af96-0de88d1b453a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9600"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "40f3ae06-b9a7-4035-aa37-3189626f01a1",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 100/100 [00:00<00:00, 470213.45it/s]\n"
     ]
    }
   ],
   "source": [
    "for i in tqdm(range(100)):\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "54ae7573-e42d-408d-9594-4f2318272ebc",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
